﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;


namespace Extractor.LibCode
{
    public class cSearchHelp : cDBService
    {
        private string xml_mapping_path = Properties.Settings.Default.pathdata + "\\searchhelp.xml";
        private XmlDocument xmldoc_srchelp_mapping;
        private string SRCTABLE = "";
        private string concatevalue = "";
        private string concatetext = "";
        private string srcdesc = "";

        private string[] fieldvalue = new string[] { };
        private string[] fieldtext = new string[] { };
        private string[] fieldselection = new string[] { };
        private string[] filtermember = new string[] { };
        private bool skipwhenempty = true;
        


        public void ShowSearcHelp()
        {
        //string sql=    

        }
        public void ShowSearcHelp(string filtername, object[] val)
        {

        }

        public void setReturnValue(DataTable dt, int idx,out string svalue,out string stext)
        {
            svalue = stext = string.Empty;
            DataRow dr = dt.Rows[idx];
            string[] sval = new string[fieldvalue.Length];
            int i = 0;
            foreach (string s in fieldvalue)
            {
                sval[i] = dr[s].ToString();                
            }
            svalue = string.Join( concatevalue,sval);
            i = 0;
            string[] stxt=new string[fieldtext.Length];

            foreach (string ss in fieldtext)
            {
                stxt[i] = dr[ss].ToString();
            }

            stext = string.Join(concatetext, stxt);
        }

        public bool getSearchHelpData(ref DataTable dt)
        {
            
            dt=new DataTable();
             string sSQL = "select " + string.Join(",", fieldselection) + " from " + SRCTABLE + " where 1=1 ";

             dt=Dbserver.ExecuteQuery(sSQL, SRCTABLE);
             return dt.Rows.Count > 0;
            
        }

        public bool getSearchHelpData(ref DataTable dt,string filterAll)
        {

            if (filterAll.Trim() != string.Empty)
            {
                dt = new DataTable();
                string sSQL = "select " + string.Join(",", fieldselection) + " from " + SRCTABLE + " where 1=1 and (";
                sSQL += string.Join(" like '%" + filterAll + "%' or ", fieldselection);
                sSQL += " like '%" + filterAll + "%' )";



                dt = Dbserver.ExecuteQuery(sSQL, SRCTABLE);
                return dt.Rows.Count > 0;
            }
            else
            {
              return  getSearchHelpData(ref dt);
            }
            return false;
        }

        public cSearchHelp(string SearchHelpName)
        {
            xmldoc_srchelp_mapping = new XmlDocument();
            if(!System.IO.File.Exists(xml_mapping_path))
                return;
            xmldoc_srchelp_mapping.Load(xml_mapping_path);
            if (xmldoc_srchelp_mapping == null)
                throw new Exception("XML Mapping Document Not Found");

            //xmlNodeList girlAddress = xmlDoc.GetElementsByTagName("gAddress");
            XmlNodeList srchelp = xmldoc_srchelp_mapping.GetElementsByTagName("searchhelp");
            foreach (XmlNode xn in srchelp)
            {
                if (xn.Attributes["id"].Value == SearchHelpName)
                {
                    SRCTABLE = xn.Attributes["table"].Value;
                    if (xn.Attributes["concatevalue"] != null)
                        concatevalue = xn.Attributes["concatevalue"].Value;
                    if (xn.Attributes["concatetext"] != null)
                        concatetext = xn.Attributes["concatetext"].Value;
                    if (xn.Attributes["desc"] != null)
                        srcdesc = xn.Attributes["concatetext"].Value;

                    foreach (XmlNode nd in xn.ChildNodes)
                    {
                        if (nd.NodeType != XmlNodeType.Comment)
                        {
                            if (nd.Name == "valuemember")
                            {
                                fieldvalue = nd.InnerText.Split('|');
                            }

                            if (nd.Name == "textmember")
                            {
                                fieldtext = nd.InnerText.Split('|');
                            }

                            if (nd.Name == "selectionmember")
                            {
                                fieldselection = nd.InnerText.Split('|');
                            }

                            if (nd.Name == "filtermember")
                            {
                                filtermember = nd.InnerText.Split('|');
                            }

                        }

                        if (filtermember.Length == 0) filtermember = fieldselection;


                        
                    }

                }
            }



        }

    }
}